﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;
using System.Web.UI.HtmlControls;

namespace RN.ERP.Web.UserControl
{
    public partial class UCChange : Utilities.BaseUserControl
    {
        #region 公共属性
        /// <summary>
        ///变更类型（企业：corp,人员：person） 
        /// </summary>
        public string ChangeType
        {
            get;
            set;
        }
        /// <summary>
        /// 企业地区
        /// </summary>
        protected string AreaCode
        {
            get
            {
                return UserInfo.AreaCode;
            }
        }

        /// <summary>
        /// 当前市场
        /// </summary>
        protected string CurrentArea
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        protected string RoleId
        {
            get
            {
                return UserInfo.RoleId;
            }
        }
        /// <summary>
        /// 页面控件编辑状态
        /// </summary>
        protected int EditStatus;

        #endregion

        #region 公共参数属性
        /// <summary>
        /// 变更记录ID
        /// </summary>
        public int ChangeSetId
        {
            get;
            set;
        }
        /// <summary>
        /// //变更类型
        /// </summary>
        public int ObjectType
        {
            get;
            set;
        }
        /// <summary>
        /// 人员或企业等变更项主键
        /// </summary>
        public string ObjectId
        {
            get;
            set;
        }
        /// <summary>
        /// 人员或企业名称
        /// </summary>
        public string ObjectName
        {
            get;
            set;
        }
        #endregion
        /*
         * 1、直接通过页面url传参，2、通过属性传参
         * 传入参数:
         * 变更集ID：changeSetId
         * 变更类型：objectType（企业：1,人员：4）
         * 人员或企业主键：objectId
         * 人员或企业名称：objectName
         */
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
                CheckFlowCode();

            }
        }
        /// <summary>
        /// 获取传递参数
        /// </summary>
        private void GetParams()
        {
            EditStatus = 0;//默认可编辑
            int changeSetId = 0;//变更集ID
            string objectId = string.Empty;//企业或人员等主键
            int objectType = 1;//变更类型（企业：1,人员：4） 
            if (Request.QueryString["changeSetId"] != null)
            {
                int.TryParse(Request.QueryString["changeSetId"].Trim(), out changeSetId);
                ChangeSetId = changeSetId;
            }
            if (Request.QueryString["objectType"] != null)
            {
                int.TryParse(Request.QueryString["objectType"].Trim(), out objectType);
                ObjectType = objectType;
            }
            if (Request.QueryString["objectId"] != null)
            {
                objectId = Request.QueryString["objectId"].Trim();
                ObjectId = objectId;
            }
            //人员或企业名称
            if (Request.QueryString["objectName"] != null)
            {
                ObjectName = Request.QueryString["objectName"].Trim();
            }
            ChangeType = Enum.GetName(typeof(ChangeTypeEnum), ObjectType);
            //用户客户端取值
            hdObjectId.Value = ObjectId;
            hdObjectType.Value = ObjectType.ToString();
            hdChangeSetId.Value = ChangeSetId.ToString();
            hdObjectName.Value = ObjectName;
            hdIsrtUserId.Value = UserInfo.CorpUserGuid;
            hdIsrtUserName.Value = UserInfo.Name;
           
        }

        /// <summary>
        /// 状态及显示内容控制
        /// </summary>
        private void CheckFlowCode()
        {
            ///企业需要上传扫描件
            if (ObjectType == 1)
            {
                ImgDiv.Visible = true;
                corpCertDiv.Visible = true; 
            }
            ///人员需要上传扫描件
            if (ObjectType == 4)
            {
                ImgDiv.Visible = true;
                imgCertDiv.Visible = true;
            }
            //添加变更信息
            if (hdChangeSetId.Value.Trim() == "0")
            {
                SetDefaultVal();
            }
            else
            {
                CheckChange();
            }
        }
        /// <summary>
        /// 页面初始化默认值
        /// </summary>
        private void SetDefaultVal()
        {
            EditStatus = 0;//可以编辑
            btnSaveDiv.Visible = true;//变更项添加按钮
            btnChangeDiv.Visible = true;//上报保存按钮
            changInfoAdd.Visible = true;//变更项添加部分
            //企业图片上传按钮
            corpCertBtn.Visible = true;
            corpPsnBtn.Visible = true;
            CorpORGBtn.Visible = true;
            CorpSWBtn.Visible = true;
            CorpAQBtn.Visible = true;
            CorpStampBtn.Visible = true;
            CorpBZJBtn.Visible = true;
            //个人图片上传按钮
            imgCardbtn.Visible = true;
        }
        /// <summary>
        /// 根据审核状态配置页面显示状态
        /// </summary>
        private void CheckChange()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                EditStatus = 1;////不可编辑
                //取得变更集可操作状态集
                List<ModelService.OperationResult> resultList = JsonConvert.DeserializeObject<List<ModelService.OperationResult>>(client.CheckChange(hdChangeSetId.Value.Trim(), RoleId, CurrentArea));
                if (resultList == null)
                {
                    return;
                }
                //可上报为真表示可编辑
                if (resultList[0].Result)
                {
                    SetDefaultVal();
                }
                //审核操作为真表示可执行审核
                if (resultList[1].Result || resultList[2].Result)
                {
                    if (UserInfo.RoleType == "Manager")
                    {
                        divOperation.Visible = true;//审核                      
                    }
                }
            }
            //操作记录
            BindDwvHistory();
        }
       
        /// <summary>
        /// 显示变更审核操作记录
        /// </summary>
        /// <param name="SetID"></param>
        private void BindDwvHistory()
        {
            divHistory.Visible = true;//显示日志
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                string result = client.AuditLogs(hdChangeSetId.Value.Trim(), CurrentArea);
                if (!string.IsNullOrWhiteSpace(result) && !result.Equals("null"))
                {
                    List<User_Log> logs = JsonConvert.DeserializeObject<List<User_Log>>(result);
                    dwvHistory.DataSource = logs;
                    dwvHistory.DataBind();
                }
            }
        }
    }
}